package com.ruoyi.mobile.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mobile.domain.LessonPrescriptionItem;
import com.ruoyi.mobile.domain.LessonPrescriptionItemLesson;
import com.ruoyi.mobile.mapper.LessonMapper;
import com.ruoyi.mobile.mapper.LessonPrescriptionItemLessonMapper;
import com.ruoyi.mobile.mapper.LessonPrescriptionItemMapper;
import com.ruoyi.mobile.model.bo.LessonInfoBo;
import com.ruoyi.mobile.model.bo.LessonPrescriptionBo;
import com.ruoyi.mobile.model.request.LessonPointPrescriptionRequest;
import com.ruoyi.mobile.model.request.LessonPrescriptionRequest;
import com.ruoyi.mobile.model.vo.WeekVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.mapper.LessonPrescriptionMapper;
import com.ruoyi.mobile.domain.LessonPrescription;
import com.ruoyi.mobile.service.ILessonPrescriptionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运动处方配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-14
 */
@Service
public class LessonPrescriptionServiceImpl implements ILessonPrescriptionService 
{
    @Autowired
    private LessonPrescriptionMapper lessonPrescriptionMapper;

    @Autowired
    private LessonPrescriptionItemMapper lessonPrescriptionItemMapper;

    @Autowired
    private LessonPrescriptionItemLessonMapper lessonPrescriptionItemLessonMapper;

    @Autowired
    private LessonMapper lessonMapper;

    /**
     * 查询运动处方配置
     * 
     * @param id 运动处方配置主键
     * @return 运动处方配置
     */
    @Override
    public LessonPrescription selectLessonPrescriptionById(Integer id)
    {
        LessonPrescription lessonPrescription = lessonPrescriptionMapper.selectLessonPrescriptionById(id);
        List<LessonPrescription.LessonPartBean> lessonPartBeans = new ArrayList<>();
        for (LessonPrescriptionItem lessonPrescriptionItem : lessonPrescription.getLessonPrescriptionItems()) {
            LessonPrescription.LessonPartBean lessonPartBean = new LessonPrescription.LessonPartBean();
            lessonPartBean.setId(lessonPrescriptionItem.getId());
            lessonPartBean.setTitle(lessonPrescriptionItem.getTitle());
            lessonPartBean.setCover(lessonPrescriptionItem.getCover());
            lessonPartBean.setItemTime(lessonPrescriptionItem.getItemTime());

            //构建dayListBean
            List<LessonPrescription.LessonPartBean.DayListBean> dayListBeans = new ArrayList<>();
            List<LessonPrescriptionItemLesson> lessonPrescriptionItemLessons = lessonPrescriptionItemLessonMapper.getListByItemId(lessonPrescriptionItem.getId());
            for (LessonPrescriptionItemLesson lessonPrescriptionItemLesson : lessonPrescriptionItemLessons) {
                LessonPrescription.LessonPartBean.DayListBean dayListBean = new LessonPrescription.LessonPartBean.DayListBean();
                dayListBean.setId(lessonPrescriptionItemLesson.getId());
                dayListBean.setTitle(lessonPrescriptionItemLesson.getTitle());

                List<LessonPrescription.LessonPartBean.DayListBean.LessonListBean> lessonListBeans = new ArrayList<>();
                String[] lessonIdStrList = lessonPrescriptionItemLesson.getLessonId().split(",");
                for (String lessonId : lessonIdStrList) {
                    if("".equals(lessonId)) {
                        continue;
                    }
                    LessonPrescription.LessonPartBean.DayListBean.LessonListBean lessonListBean = new LessonPrescription.LessonPartBean.DayListBean.LessonListBean();
                    lessonListBean.setLessonId(lessonId);
                    lessonListBeans.add(lessonListBean);
                }
                dayListBean.setLessonList(lessonListBeans);
                dayListBeans.add(dayListBean);
            }

            lessonPartBean.setDayList(dayListBeans);

            lessonPartBeans.add(lessonPartBean);
        }
        lessonPrescription.setLessonPart(lessonPartBeans);
        return lessonPrescription;
    }

    /**
     * 查询某一周的运动处方配置
     *
     * @param week 周次
     * @return 运动处方配置
     */
    @Override
    public LessonPrescription selectLessonPrescriptionByWeek(Integer week) {
        LessonPrescription lessonPrescription = lessonPrescriptionMapper.selectLessonPrescriptionByWeek(week);
        List<LessonPrescription.LessonPartBean> lessonPartBeans = new ArrayList<>();
        for (LessonPrescriptionItem lessonPrescriptionItem : lessonPrescription.getLessonPrescriptionItems()) {
            LessonPrescription.LessonPartBean lessonPartBean = new LessonPrescription.LessonPartBean();
            lessonPartBean.setId(lessonPrescriptionItem.getId());
            lessonPartBean.setTitle(lessonPrescriptionItem.getTitle());
            lessonPartBean.setCover(lessonPrescriptionItem.getCover());
            lessonPartBean.setItemTime(lessonPrescriptionItem.getItemTime());

            //构建dayListBean
            List<LessonPrescription.LessonPartBean.DayListBean> dayListBeans = new ArrayList<>();
            List<LessonPrescriptionItemLesson> lessonPrescriptionItemLessons = lessonPrescriptionItemLessonMapper.getListByItemId(lessonPrescriptionItem.getId());
            for (LessonPrescriptionItemLesson lessonPrescriptionItemLesson : lessonPrescriptionItemLessons) {
                LessonPrescription.LessonPartBean.DayListBean dayListBean = new LessonPrescription.LessonPartBean.DayListBean();
                dayListBean.setId(lessonPrescriptionItemLesson.getId());
                dayListBean.setTitle(lessonPrescriptionItemLesson.getTitle());

                List<LessonPrescription.LessonPartBean.DayListBean.LessonListBean> lessonListBeans = new ArrayList<>();
                String[] lessonIdStrList = lessonPrescriptionItemLesson.getLessonId().split(",");
                for (String lessonId : lessonIdStrList) {
                    if("".equals(lessonId)) {
                        continue;
                    }
                    LessonPrescription.LessonPartBean.DayListBean.LessonListBean lessonListBean = new LessonPrescription.LessonPartBean.DayListBean.LessonListBean();
                    lessonListBean.setLessonId(lessonId);
                    lessonListBeans.add(lessonListBean);
                }
                dayListBean.setLessonList(lessonListBeans);
                dayListBeans.add(dayListBean);
            }

            lessonPartBean.setDayList(dayListBeans);

            lessonPartBeans.add(lessonPartBean);
        }
        lessonPrescription.setLessonPart(lessonPartBeans);
        return lessonPrescription;
    }

    @Override
    public List<WeekVo> selectAllWeek() {
        List<WeekVo> list = lessonPrescriptionMapper.selectAllWeek();
        for (WeekVo weekVo : list) {
            weekVo.setWeekName("第" + weekVo.getWeek().toString() + "周");
        }
        return list;
    }

    /**
     * 查询运动处方配置列表
     * 
     * @param lessonPrescription 运动处方配置
     * @return 运动处方配置
     */
    @Override
    public List<LessonPrescriptionBo> selectLessonPrescriptionList(LessonPrescription lessonPrescription)
    {
        List<LessonPrescriptionBo> list = lessonPrescriptionMapper.selectLessonPrescriptionList(lessonPrescription);
        return list;
    }

    /**
     * 新增运动处方配置
     * 
     * @param lessonPrescriptionRequest 运动处方配置
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLessonPrescription(LessonPrescriptionRequest lessonPrescriptionRequest)
    {
        //先检查这个周次的课程是否有设置
        LessonPrescription weekLessonPrescription = lessonPrescriptionMapper.selectLessonPrescriptionByWeek(lessonPrescriptionRequest.getWeek());
        if (weekLessonPrescription != null) {
            throw new ServiceException("该周次运动处方已经设置，请勿重复");
        }


        LessonPrescription lessonPrescription = new LessonPrescription();
        lessonPrescription.setWeek(lessonPrescriptionRequest.getWeek());
        lessonPrescription.setPrice(lessonPrescriptionRequest.getPrice());
        lessonPrescription.setDesc(lessonPrescriptionRequest.getDesc());
        lessonPrescription.setProhibition(lessonPrescriptionRequest.getProhibition());
        lessonPrescription.setAttention(lessonPrescriptionRequest.getAttention());
        lessonPrescription.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        lessonPrescription.setCreateTime(DateUtils.getNowDate());
        lessonPrescriptionMapper.insertLessonPrescription(lessonPrescription);


        if (!lessonPrescriptionRequest.getLessonPart().isEmpty()) {
            for (LessonPrescriptionRequest.LessonPartBean lessonPartBean : lessonPrescriptionRequest.getLessonPart()) {
                LessonPrescriptionItem lessonPrescriptionItem = new LessonPrescriptionItem();
                if (StrUtil.isBlank(lessonPartBean.getCover())) {
                    throw new ServiceException("请选择封面");
                }
                if (StrUtil.isBlank(lessonPartBean.getTitle())) {
                    throw new ServiceException("请选择标题");
                }
                if (lessonPartBean.getItemTime() == null || lessonPartBean.getItemTime() < 0) {
                    throw new ServiceException("请选择课程时长");
                }
                lessonPrescriptionItem.setCover(lessonPartBean.getCover());
                lessonPrescriptionItem.setTitle(lessonPartBean.getTitle());
                lessonPrescriptionItem.setItemTime(lessonPartBean.getItemTime());
                lessonPrescriptionItem.setPrescriptionId(lessonPrescription.getId());
                lessonPrescriptionItemMapper.insertLessonPrescriptionItem(lessonPrescriptionItem);

                for (LessonPrescriptionRequest.LessonPartBean.DayListBean dayListBean : lessonPartBean.getDayList()) {
                    LessonPrescriptionItemLesson lessonPrescriptionItemLesson = new LessonPrescriptionItemLesson();
                    lessonPrescriptionItemLesson.setItemId(lessonPrescriptionItem.getId());
                    lessonPrescriptionItemLesson.setPrescriptionId(lessonPrescription.getId());
                    lessonPrescriptionItemLesson.setTitle(dayListBean.getTitle());

                    String lessonId = "";
                    if (!dayListBean.getLessonList().isEmpty()) {
                        for (LessonPrescriptionRequest.LessonPartBean.DayListBean.LessonListBean lessonListBean : dayListBean.getLessonList()) {
                            if (lessonListBean.getLessonId() == null) {
                                continue;
                            }
                            lessonId = "".equals(lessonId) ? lessonListBean.getLessonId().toString() : lessonId + "," + lessonListBean.getLessonId().toString();
                        }
                    }
                    lessonPrescriptionItemLesson.setLessonId(lessonId);
                    lessonPrescriptionItemLessonMapper.insertLessonPrescriptionItemLesson(lessonPrescriptionItemLesson);

                }

            }
        }

        return 1;
    }

    /**
     * 修改运动处方配置
     * 
     * @param lessonPrescriptionRequest 运动处方配置
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLessonPrescription(LessonPrescriptionRequest lessonPrescriptionRequest)
    {
        //先检查这个周次的课程是否有设置
        LessonPrescription weekLessonPrescription = lessonPrescriptionMapper.selectLessonPrescriptionByWeek(lessonPrescriptionRequest.getWeek());
        if (weekLessonPrescription != null  && weekLessonPrescription.getWeek() != lessonPrescriptionRequest.getWeek()) {
            throw new ServiceException("该周次运动处方已经设置，请勿重复");
        }

        LessonPrescription lessonPrescription = new LessonPrescription();
        lessonPrescription.setId(lessonPrescriptionRequest.getId());
        lessonPrescription.setWeek(lessonPrescriptionRequest.getWeek());
        lessonPrescription.setPrice(lessonPrescriptionRequest.getPrice());
        lessonPrescription.setDesc(lessonPrescriptionRequest.getDesc());
        lessonPrescription.setProhibition(lessonPrescriptionRequest.getProhibition());
        lessonPrescription.setAttention(lessonPrescriptionRequest.getAttention());
        lessonPrescription.setUpdateBy(SecurityUtils.getLoginUser().getUserId().toString());
        lessonPrescription.setUpdateTime(DateUtils.getNowDate());
        lessonPrescriptionMapper.updateLessonPrescription(lessonPrescription);

        //检查这个lessonPrescription底下有多少个item，有些item可能要被删掉
        List<Integer> itemIdList = lessonPrescriptionItemMapper.getIdListByPrescriptionId(lessonPrescriptionRequest.getId());

        if (!lessonPrescriptionRequest.getLessonPart().isEmpty()) {
            for (LessonPrescriptionRequest.LessonPartBean lessonPartBean : lessonPrescriptionRequest.getLessonPart()) {
                LessonPrescriptionItem lessonPrescriptionItem = new LessonPrescriptionItem();
                lessonPrescriptionItem.setCover(lessonPartBean.getCover());
                lessonPrescriptionItem.setTitle(lessonPartBean.getTitle());
                lessonPrescriptionItem.setItemTime(lessonPartBean.getItemTime());
                lessonPrescriptionItem.setPrescriptionId(lessonPrescription.getId());
                if (lessonPartBean.getId() != null) {
                    //编辑lessonPrescriptionItem
                    if (itemIdList.contains(lessonPartBean.getId())) {
                        itemIdList.remove(lessonPartBean.getId());
                    }
                    lessonPrescriptionItem.setId(lessonPartBean.getId());
                    lessonPrescriptionItemMapper.updateLessonPrescriptionItem(lessonPrescriptionItem);
                } else {
                    //新增lessonPrescriptionItem
                    lessonPrescriptionItemMapper.insertLessonPrescriptionItem(lessonPrescriptionItem);
                }

                //检查这个lessonPrescriptionItem底下有多少个lesson，有些lesson可能要被删掉
                List<Integer> itemLessonIdList = lessonPrescriptionItemLessonMapper.getIdListByItemId(lessonPrescriptionItem.getId());

                for (LessonPrescriptionRequest.LessonPartBean.DayListBean dayListBean : lessonPartBean.getDayList()) {
                    LessonPrescriptionItemLesson lessonPrescriptionItemLesson = new LessonPrescriptionItemLesson();
                    lessonPrescriptionItemLesson.setItemId(lessonPrescriptionItem.getId());
                    lessonPrescriptionItemLesson.setPrescriptionId(lessonPrescription.getId());
                    lessonPrescriptionItemLesson.setTitle(dayListBean.getTitle());

                    String lessonId = "";
                    if (!dayListBean.getLessonList().isEmpty()) {
                        for (LessonPrescriptionRequest.LessonPartBean.DayListBean.LessonListBean lessonListBean : dayListBean.getLessonList()) {
                            if (lessonListBean.getLessonId() == null) {
                                continue;
                            }
                            lessonId = "".equals(lessonId) ? lessonListBean.getLessonId().toString() : lessonId + "," + lessonListBean.getLessonId().toString();
                        }
                    }
                    lessonPrescriptionItemLesson.setLessonId(lessonId);
                    //检查是新增还是编辑
                    if (dayListBean.getId() != null) {
                        //编辑lessonPrescriptionItemLesson
                        if (itemLessonIdList.contains(dayListBean.getId())) {
                            itemLessonIdList.remove(dayListBean.getId());
                        }
                        lessonPrescriptionItemLesson.setId(dayListBean.getId());
                        lessonPrescriptionItemLessonMapper.updateLessonPrescriptionItemLesson(lessonPrescriptionItemLesson);
                    } else {
                        //增加lessonPrescriptionItemLesson
                        lessonPrescriptionItemLessonMapper.insertLessonPrescriptionItemLesson(lessonPrescriptionItemLesson);
                    }

                }

                if (!itemLessonIdList.isEmpty()) {
                    lessonPrescriptionItemLessonMapper.deleteByIds(itemLessonIdList);
                }
            }
        }

        if (!itemIdList.isEmpty()) {
            lessonPrescriptionItemMapper.deleteByIds(itemIdList);
            lessonPrescriptionItemLessonMapper.deleteByItemIds(itemIdList);
        }


        return 1;
    }

    /**
     * 批量删除运动处方配置
     * 
     * @param ids 需要删除的运动处方配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteLessonPrescriptionByIds(Integer[] ids)
    {
        lessonPrescriptionMapper.deleteLessonPrescriptionByIds(ids);
        lessonPrescriptionItemMapper.deleteLessonPrescriptionByPrescriptionIds(ids);
        return lessonPrescriptionItemLessonMapper.deleteLessonPrescriptionByPrescriptionIds(ids);
    }



}
